package com.melon.modules.exception.entity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.DefaultErrorViewResolver;
import org.springframework.boot.autoconfigure.web.ErrorViewResolver;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.web.servlet.ErrorPageRegistrar;
import org.springframework.boot.web.servlet.ErrorPageRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: xjt2016
 * Date: 2016/8/16
 * Time: 20:25
 */
//@Configuration
class ExceptionConfiguration {
    private static final Logger log = LoggerFactory.getLogger(ExceptionConfiguration.class);


    //@Bean
    public ErrorPageRegistrar errorPageRegistrar() {
        return new MyErrorPageRegistrar();
    }

    //@Bean
    public ErrorViewResolver MyErrorViewResolver() {
        return new MyErrorViewResolver();
    }

    private static class MyErrorPageRegistrar implements ErrorPageRegistrar {

        @Override
        public void registerErrorPages(ErrorPageRegistry registry) {
            // registry.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
        }

    }

    public class MyErrorViewResolver implements ErrorViewResolver {

        @Override
        public ModelAndView resolveErrorView(HttpServletRequest request,
                                             HttpStatus status, Map<String, Object> model) {
            // Use the request or status to optionally return a ModelAndView
            log.debug("this is a custom error view :{}", "MyErrorViewResolver");
            return new ModelAndView("error/error", model);
        }

    }

    public class MyErrorViewResolver2 extends DefaultErrorViewResolver {


        public MyErrorViewResolver2(ApplicationContext applicationContext, ResourceProperties resourceProperties) {
            super(applicationContext, resourceProperties);
        }
    }

}
